package com.heima.leetcode.practice;

/**
 * 检测 大写字母 520
 *
 * @author 勾新杰
 * @version 1.0
 * @date 2024/10/23 8:54
 */
public class E520 {

    /**
     * <h3>方法一：大小写转换</h3>
     *
     * @param word 单词
     * @return 合法返回true，否则false
     */
    public static boolean detectCapitalUse1(String word) {
        if (word.length() == 1) {
            return true;
        }
        String substring = word.substring(1); // 包前不包后，一个参数就是截取到末尾
        String lowerCase = substring.toLowerCase();
        String upperCase = substring.toUpperCase();
        if ('A' <= word.charAt(0) && word.charAt(0) <= 'Z') {
            return substring.equals(lowerCase) || substring.equals(upperCase);
        } else {
            return substring.equals(lowerCase);
        }
    }

    /**
     * <h3>方法二：情况列举</h3>
     *
     * @param word 单词
     * @return 合法返回true，否则返回false
     */
    public static boolean detectCapitalUse2(String word) {
        if (word.length() == 1) {
            return true;
        }
        char c = word.charAt(0);
        if (c >= 'a') {
            for (int i = 1; i < word.length(); i++) {
                if (word.charAt(i) < 'a') {
                    return false;
                }
            }
            return true;
        } else {
            if (word.length() == 2) {
                return true;
            }
            char t = word.charAt(1);
            String substring = word.substring(2);
            if (t >= 'a') {
                return substring.equals(substring.toLowerCase());
            } else {
                return substring.equals(substring.toUpperCase());
            }
        }
    }

    /**
     * <h3>方法三：方法二简化</h3>
     *
     * @param word 单词
     * @return 合法返回true，否则返回false
     */
    public static boolean detectCapitalUse3(String word) {
        int length = word.length();
        if (length == 1) return true;

        boolean firstIsUpper = Character.isUpperCase(word.charAt(0));
        boolean secondIsUpper = Character.isUpperCase(word.charAt(1));

        if (!firstIsUpper && secondIsUpper) return false;

        for (int i = 2; i < word.length(); i++) {
            if (firstIsUpper) {
                if (secondIsUpper) {
                    if (Character.isLowerCase(word.charAt(i))) return false;
                } else {
                    if (Character.isUpperCase(word.charAt(i))) return false;
                }
            } else {
                if (Character.isUpperCase(word.charAt(i))) return false;
            }
        }
        return true;
    }

    /**
     * <h3>方法四：判断一致性优化</h3>
     * @param word 单词
     * @return 合法返回true，否则返回false
     */
    public static boolean detectCapitalUse4(String word) {
        int length = word.length();
        if (length == 1) return true;

        if (Character.isUpperCase(word.charAt(0))){
            boolean secondIsUpper = Character.isUpperCase(word.charAt(1));
            for (int i = 2; i < length; i++) {
                if (secondIsUpper != Character.isUpperCase(word.charAt(i))) return false;
            }
        }else {
            for (int i = 1; i < length; i++){
                if (Character.isUpperCase(word.charAt(i))) return false;
            }
        }
        return true;
    }
}
